package com.wu.client.controller;

import cn.hutool.core.bean.BeanUtil;
import com.wu.client.cache.newData.NewAlbumCache;
import com.wu.client.cache.newData.NewSongCache;
import com.wu.client.constant.ResTypeEnum;
import com.wu.client.service.NewService;
import com.wu.client.vo.res.PageResp;
import com.wu.client.vo.res.album.AlbumBaseResp;
import com.wu.client.vo.res.home.ReleaseData;
import com.wu.client.vo.res.song.SongBaseResp;
import com.wu.common.convertion.result.Result;
import com.wu.common.convertion.result.Results;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Cousin
 * @date 2025/3/28
 * @Description
 */
@RestController
@RequestMapping("new")
@RequiredArgsConstructor
public class NewController {
    private final NewService newService;
    @GetMapping("/songs")
    public Result<PageResp<SongBaseResp>> newSongs(@RequestParam(required = false) Long userId,@RequestParam Integer page, @RequestParam Integer limit) {
        return Results.success(newService.getNewSongs(userId,page,limit));
    }
    @GetMapping("/albums")
    public Result<PageResp<AlbumBaseResp>> newAlbums(
            @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit) {
       return Results.success(newService.getNewAlbums(page,limit));
    }
    @GetMapping("/home")
    private Result<List<ReleaseData>> NewReleases(){
        NewSongCache newSongCache = NewSongCache.getInstance();
        List<ReleaseData> data = new ArrayList<>(6);
        List<ReleaseData> releaseDataList = newSongCache.getInPage(1, 3)
                .stream().map(songMongo -> {
                    ReleaseData releaseData = BeanUtil.toBean(songMongo, ReleaseData.class);
                    releaseData.setType(ResTypeEnum.SONG.getCode());
                    return releaseData;
                }).toList();
        NewAlbumCache newAlbumCache = NewAlbumCache.getInstance();
        List<ReleaseData> albumReleaseDataList = newAlbumCache.getInPage(1, 3)
                .stream().map(albumMongo -> {
                    ReleaseData releaseData = BeanUtil.toBean(albumMongo, ReleaseData.class);
                    releaseData.setType(ResTypeEnum.ALBUM.getCode());
                    return releaseData;
                }).toList();
        data.addAll(releaseDataList);
        data.addAll(albumReleaseDataList);
        return Results.success(data);
    }
}
